package User;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import Expense.ExpensePane;
import Inventory.InventoryPane;
import Inventory.InventoryRecord;
import Login.Button;
import Login.DBHandler;
import Login.User;
import Login.MainFrame;
import Sales.SalePane;
public class UserMain extends JPanel implements ActionListener, MouseListener, MouseMotionListener{
        private JPanel cards;
        private CardLayout cl;
        private BufferedImage bg;
        private Button salesBtn, inBtn, expBtn, usBtn, logBtn;
        private Timer time;
        private int userType;
        private JFrame frame;
        private DBHandler db;
        private JPanel parent;
        private CardLayout mainCards;
        private JFrame mainFrame;
        
        public UserMain(DBHandler db, int userType, String username, ArrayList<User> users, JFrame frame, JPanel parent, CardLayout c){
                
        		this.mainFrame =frame;
        		this.parent=parent;
        		this.mainCards = c;
        		cards=new JPanel(cl=new CardLayout());
                this.db=db;
        		this.userType = userType;
                this.setLayout(null);
                Date date = new Date();
                
        		ArrayList<InventoryRecord> Supplies = getDBinventoryItems();
        		int ctr=0;
        		for(int i=0; i<Supplies.size(); i++){
        			if(Supplies.get(i).getBalance()<=15)
        				ctr++;
        		}
        		
                String[] months={"January", "February", "March", "April","May", "June",
                                "July", "August", "September", "October", "November", "December"};

                JLabel welcome = new JLabel("Welcome " + username +"!");
                JLabel today = new JLabel("Today is: " + months[date.getMonth()]+" "+ date.getDate() + " , " + (date.getYear()+1900));
                Font font = new Font("Calibri", Font.PLAIN, 20);
                try {
                    salesBtn = new Button("images/b1.png", "images/b1a.png", "images/b1.png");
                    inBtn = new Button("images/b2.png", "images/b2a.png", "images/b2.png");
                    expBtn = new Button("images/b3.png", "images/b3a.png", "images/b3.png");
                    usBtn = new Button("images/b5.png", "images/b5a.png", "images/b5.png");
                    logBtn = new Button("images/b7.png", "images/b7a.png", "images/b7.png");
                    
                    switch(userType){
                    case 4: bg=ImageIO.read(new File("images/main2.png"));
                                    
                    				JLabel prod = new JLabel("You have " + ctr+ " products/s that are low in supply");
                                    prod.setFont(font);
                                    prod.setBounds(145, 240, 500, 100 );
                                    this.add(prod);
                                    break;
                    case 3: bg=ImageIO.read(new File("images/main1.png"));
                    				JLabel prod2 = new JLabel("You have " + ctr+" products/s that are low in supply");
                    				prod2.setFont(font);
                    				prod2.setBounds(145, 240, 500, 100 );
                    				this.add(prod2);
                    				break;
                    case 5: bg=ImageIO.read(new File("images/main3.png"));
                    }
                    
            } catch (IOException e) {
                    System.out.print("Image not found");
                    e.printStackTrace();
            }
                welcome.setFont(font);
                today.setFont(font);
                
                welcome.setBounds(145, 145, 500, 100);
                today.setBounds(145, 185, 500, 100);
                cards.add(this, "0");
                createFrame();
                this.add(welcome);
                this.add(today);
                this.addMouseListener(this);
                this.addMouseMotionListener(this);
                time=new Timer(300, this);
                switch(userType){
                case 4:	SalePane sp=new SalePane(db, cards, cl, bg, this.frame, 4)	;
                		UserPane up=new UserPane(db, cards, cl, bg, this.frame);
                    	cards.add(sp, "sales");
                    	cards.add(up, "users");
	
                case 3:InventoryPane in=new InventoryPane(db, cards, cl, bg, this.frame, userType);
                	   ExpensePane ep=new ExpensePane(db, cards, cl, bg, this.frame, userType)	;
                	   cards.add(in, "inventory");
                	   cards.add(ep, "expense");
                   		
                	   break;
                case 5:	SalePane sp1=new SalePane(db, cards, cl, bg, this.frame, 5)	;
                		cards.add(sp1, "sales");
            	
                }
        }
        public void createFrame(){
                
                frame = new JFrame("NiSoft");
                frame.add(cards);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(bg.getWidth()+15, bg.getHeight()+38);
                frame.setLocationRelativeTo(null);
                
        }

    	public ArrayList<InventoryRecord> getDBinventoryItems() {

    		ArrayList<InventoryRecord> Records = new ArrayList<InventoryRecord>();
    		ResultSet RS;
    		int ctr = 0;

    		RS = db.MySQLSelect("SELECT InventoryID,InventoryIn,InventoryOut,TimeRecorded, unit, productname from Inventory,Product where TimeRecorded=Date(now()) AND prCode=ProductCode;");

    		try {
    			while (RS.next()) {
    				Records.add(new InventoryRecord(RS.getString(1), RS
    						.getString(5), RS.getString(6), RS.getDate(4), RS
    						.getInt(2), RS.getInt(3)));
    				ctr++;
    			}
    			RS.close();
    			
    		} catch (SQLException SQLe) {
    			// TODO Auto-generated catch block
    		}

    		return Records;
    	}


        @Override
        public void actionPerformed(ActionEvent m) {
                // TODO Auto-generated method stub
                repaint();
        }
        
        public void paint(Graphics g){
                super.paint(g);
                Graphics2D g2D = (Graphics2D)g;
                time.start();
                g2D.drawImage(bg, 0, 0, null);
                switch(userType){
                
                case 4: 		g2D.drawImage(usBtn.getCurImg(), 497, 8, null);
                                g2D.drawImage(salesBtn.getCurImg(), 145, 8, null);
                                g2D.drawImage(inBtn.getCurImg(), 269, 8, null);
                                g2D.drawImage(expBtn.getCurImg(), 383, 8, null);
                                break;
                case 3: 		g2D.drawImage(inBtn.getCurImg(), 138, 8, null);
                                g2D.drawImage(expBtn.getCurImg(), 243, 8, null);
                                break;
                case 5: 		g2D.drawImage(salesBtn.getCurImg(), 145, 8, null);
                               
                }
                g2D.drawImage(logBtn.getCurImg(), 611, 8, null);
                
        }
        @Override
        public void mouseDragged(MouseEvent m) {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void mouseMoved(MouseEvent m) {
                // TODO Auto-generated method stub
                switch(userType){
                
                case 4:
                        if (m.getX() >= 151&& m.getX()<=229&&m.getY()>=8&&m.getY()<=86)//within the boundaries of sales btn                     
                                salesBtn.setCurImg(1);
                        else if (m.getX() >= 275&& m.getX()<=354&&m.getY()>=7&&m.getY()<=88)//within the boundaries of inventory btn                    
                                inBtn.setCurImg(1);
                        else if (m.getX() >= 390&& m.getX()<=469&&m.getY()>=8&&m.getY()<=86)//within the boundaries of exp btn                  
                                expBtn.setCurImg(1);
                        else if (m.getX() >= 504&& m.getX()<=581&&m.getY()>=7&&m.getY()<=85)//within the boundaries of us btn                   
                                usBtn.setCurImg(1);
                        else if (m.getX() >= 617&& m.getX()<=698&&m.getY()>=9&&m.getY()<=85)//within the boundaries of log out btn                      
                                logBtn.setCurImg(1);
                
                        else{
                                salesBtn.setCurImg(0);
                                inBtn.setCurImg(0);
                                expBtn.setCurImg(0);
                                usBtn.setCurImg(0);
                                logBtn.setCurImg(0);
                        }
                        break;
                case 3:
                        if (m.getX() >= 143&& m.getX()<=223&&m.getY()>=7&&m.getY()<=86)//within the boundaries of inventory btn                 
                                inBtn.setCurImg(1);
                        else if (m.getX() >= 249&& m.getX()<=327&&m.getY()>=7&&m.getY()<=86)//within the boundaries of exp btn                  
                                expBtn.setCurImg(1);
                        else if (m.getX() >= 617&& m.getX()<=698&&m.getY()>=9&&m.getY()<=85)//within the boundaries of log out btn                      
                                logBtn.setCurImg(1);
                        else{
                                inBtn.setCurImg(0);
                                expBtn.setCurImg(0);
                                logBtn.setCurImg(0);
                        }
                        break;
                        
                case 5:
                        if (m.getX() >= 151&& m.getX()<=229&&m.getY()>=8&&m.getY()<=86)//within the boundaries of sales btn                     
                                salesBtn.setCurImg(1);
                        else if (m.getX() >= 617&& m.getX()<=698&&m.getY()>=9&&m.getY()<=85)//within the boundaries of log out btn                      
                                logBtn.setCurImg(1);
                        else
                                salesBtn.setCurImg(0);
                                logBtn.setCurImg(0);
                        
                }
                
        }

        @Override
        public void mouseClicked(MouseEvent m) {
                // TODO Auto-generated method stub
       
    	switch(userType){
                case 4:
                		if (m.getX() >= 151&& m.getX()<=229&&m.getY()>=8&&m.getY()<=86)//within the boundaries of sales btn			
                			cl.show(cards, "sales");
                		
                		else if (m.getX() >= 275&& m.getX()<=354&&m.getY()>=7&&m.getY()<=88)//within the boundaries of inventory btn                  
                			cl.show(cards, "inventory");
                		else if (m.getX() >= 390&& m.getX()<=469&&m.getY()>=8&&m.getY()<=86)//within the boundaries of exp btn                  
                			cl.show(cards, "expense");

                        else if (m.getX() >= 504&& m.getX()<=581&&m.getY()>=7&&m.getY()<=85)//within the boundaries of us btn                       
                             cl.show(cards, "users");
                        
                       
                        break;
                case 3:
                        if (m.getX() >= 143&& m.getX()<=223&&m.getY()>=7&&m.getY()<=86)//within the boundaries of inventory btn                       
                 			cl.show(cards, "inventory");
                        else if (m.getX() >= 249&& m.getX()<=327&&m.getY()>=7&&m.getY()<=86)//within the boundaries of exp btn                  
                 			cl.show(cards, "expense");
                        break;
                                
                        
                case 5:
                		if (m.getX() >= 151&& m.getX()<=229&&m.getY()>=8&&m.getY()<=86)//within the boundaries of sales btn			
                			cl.show(cards, "sales");
                		
                }
        
        if (m.getX() >= 617&& m.getX()<=698&&m.getY()>=9&&m.getY()<=85){//within the boundaries of log out btn                  
        	final JFrame frame2  = new JFrame("Confirm Action");

    		JLabel label  = new JLabel();
    		JLabel label2 = new JLabel("Are you sure you want to log out?");
    		JButton yes = new JButton("Yes");
    		JButton no = new JButton("No");
    		yes.addActionListener(
    				new ActionListener(){
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    						mainCards.show(parent, "1");
    						mainFrame.setVisible(true);
    						frame.dispose();
    		                frame2.dispose();
    					}
    				}
    		);
    		yes.setIcon(new ImageIcon("Images/yes.png"));
    		no.setIcon(new ImageIcon("Images/no.png"));
    		
    		no.addActionListener(
    				new ActionListener(){
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    						frame2.dispose();
    					}
    				}
    		);
    		
    		label2.setFont(new Font(Font.SERIF, Font.PLAIN, 17));
    		
    		JPanel panel  = new JPanel();
    		ImageIcon confirm;
    		confirm= new ImageIcon("Images/confirmaction.png");
    		label.setIcon(confirm);
    		
    		panel.add(label);
    		panel.add(label2);
    		panel.add(yes);
    		panel.add(no);

    		label.setBounds(26, 20, 120, 120);
    		label2.setBounds(145, 40, 250, 30);
    		yes.setBounds(160, 105, 80, yes.getPreferredSize().height);
    		no.setBounds(260, 105, 80, no.getPreferredSize().height+2);
    		
    		panel.setLayout(null);
    		frame2.add(panel);
    		frame2.setVisible(true);
    		frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		frame2.setSize(400, 200);
    		frame2.setLocationRelativeTo(null);
    	 	
    	}	   
        	
        }

        @Override
        public void mouseEntered(MouseEvent m) {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void mouseExited(MouseEvent m) {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void mousePressed(MouseEvent m) {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void mouseReleased(MouseEvent m) {
                // TODO Auto-generated method stub
                
        }


}